package com.whfc.wxmp.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.emp.dto.AppWarnEmpRecordDTO;
import com.whfc.emp.dto.AppWarnEmpRecordDetailDTO;
import com.whfc.emp.dto.AppWarnEmpRecordNumDTO;
import com.whfc.emp.dto.train.AppWxWarnEmpRecordNumDTO;
import com.whfc.emp.dto.train.AppWxWarnEmpRecordNumDTOV2;
import com.whfc.emp.param.AppWarnHandleParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.emp.service.WxEmpWarnService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 报警
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020/6/30 9:59
 */
@RestController
@RequestMapping("/mp/api/emp/warn")
public class WxEmpWarnController extends BaseController {

    @Autowired
    private WxEmpWarnService wxEmpWarnService;


    /**
     * 2.0 人员管理模块报警记录
     * @param wxUser
     * @param deptId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    public Result empWarnList(@CurrentWxUser WxUser wxUser,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              @RequestParam(value="state",required = false) Integer state,
                              @RequestParam(value="ruleType",required = false) Integer ruleType,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime", required = false) Date startTime,
                              @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime", required = false) Date endTime
    ) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取人员报警记录列表", RequestConetxtUtil.getParameters());
        PageData<AppWarnEmpRecordDTO> pageData = wxEmpWarnService.empWarnList(deptId, pageNum, pageSize, state,ruleType,startTime,endTime);
        return ResultUtil.success(pageData);
    }

    /**
     * 查询人员报警消息详情
     *
     * @param loginUser
     * @param warnId
     * @return
     */
    @GetMapping("detail")
    public Result getEmpWarnMsgDetail(@CurrentWxUser WxUser loginUser,
                                      @RequestParam("warnId") Integer warnId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询人员报警消息详情", RequestConetxtUtil.getParameters());
        AppWarnEmpRecordDetailDTO warnDetail = wxEmpWarnService.getEmpWarnDetail(warnId);
        return ResultUtil.success(warnDetail);
    }

    /**
     * 处理人员报警消息
     *
     * @param loginUser
     * @param body
     * @return
     */
    @PostMapping("handle")
    public Result handleEmpWarn(@CurrentWxUser WxUser loginUser,
                                @RequestBody @Validated AppWarnHandleParam body) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "处理人员报警消息", JSONUtil.toString(body));
        wxEmpWarnService.handleEmpWarn(loginUser, body);
        return ResultUtil.success();
    }


    /**
     * 2.0 报警数量统计
     * @param user
     * @param deptId
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/record/num")
    public R<AppWxWarnEmpRecordNumDTOV2> num(@CurrentWxUser WxUser user,
                                           @RequestParam("deptId") Integer deptId,
//                                           @RequestParam(value="state",required = false) Integer state,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime", required = false) Date startTime,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime", required = false) Date endTime) {
        logger.info("人员报警数量,loginer:{},pageSize:{},startDate:{},endDate:{}",
                user.getName(), deptId,  startTime, endTime);
//        Date startTime = null;
//        Date endTime = null;
//        if (startDate != null) {
//            startTime = DateUtil.getDateBegin(startDate);
//        }
//        if (endDate != null) {
//            endTime = DateUtil.getDateEnd(endDate);
//        }
        AppWxWarnEmpRecordNumDTOV2 data = wxEmpWarnService.num(deptId, startTime, endTime);
        return R.success(data);
    }
}
